package PracticasPL.Practica1;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class App 
{
    public static Invitacion parserXML(String file)
    {

    	Invitacion inv = new Invitacion();
    	try {
    		File f = new File(file);
    		
    		/* -------------------------------------------------------------- */
    		/* A partir de aqui se parsea el XML                              */
    		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    		DocumentBuilder db = dbf.newDocumentBuilder();
    		Document doc = db.parse(f); 
    		doc.getDocumentElement().normalize();
    		NodeList nodeLst = doc.getElementsByTagName("invitation");
    		Node root = nodeLst.item(0);
    		if(root.getNodeType() == Node.ELEMENT_NODE)
    		{
    			Element fstElmnt = (Element) root;
    			NodeList front = fstElmnt.getElementsByTagName("front");
    			Element fstFront = (Element) front.item(0);
    			NodeList toList = fstFront.getElementsByTagName("to");
    			Element fstTo = (Element)toList.item(0);
    			NodeList to = fstTo.getChildNodes();
    			String TO = ((Node)to.item(0)).getNodeValue();
    			inv.setTo(TO);
    			
    			
    			NodeList dateList = fstFront.getElementsByTagName("date");
    			Element fstDate = (Element)dateList.item(0);
    			NodeList date = fstDate.getChildNodes();
    			String Date = ((Node)date.item(0)).getNodeValue();
     			inv.setDate(Date);
    			
    			NodeList whereList = fstFront.getElementsByTagName("where");
    			Element fstWhere = (Element)whereList.item(0);
    			NodeList where = fstWhere.getChildNodes();
    			String Where = ((Node)where.item(0)).getNodeValue();
    			inv.setWhere(Where);
    			
    			NodeList whyList = fstFront.getElementsByTagName("why");
    			Element fstWhy = (Element)whyList.item(0);
    			NodeList why = fstWhy.getChildNodes();
    			String Why = ((Node)why.item(0)).getNodeValue();
    			inv.setWhy(Why);
    			
    			NodeList body = fstElmnt.getElementsByTagName("body");
    			Element fstPar = (Element)body.item(0);
    			NodeList parList = fstPar.getElementsByTagName("par");
    			String bodyTxt = "";
    			for(int i = 0; i < parList.getLength(); i++ )
    			{
    				bodyTxt = bodyTxt + "<P>";
    				bodyTxt = bodyTxt + ((Node)parList.item(i).getFirstChild()).getNodeValue();  			   		
    				bodyTxt = bodyTxt + "</P>\n";
    			}
    			
    			inv.setBody(bodyTxt);
    			
    			NodeList back = fstElmnt.getElementsByTagName("back");
    			Element fstBack = (Element) back.item(0);
    			NodeList signatureList = fstBack.getElementsByTagName("signature");
    			Element fstSignature = (Element)signatureList.item(0);
    			NodeList signature = fstSignature.getChildNodes();
    			String Sign = ((Node)signature.item(0)).getNodeValue();
    			inv.setFrom(Sign);
    		}
    		
    	}
    	catch (Exception e) 
    	{
    		e.printStackTrace();
    		
    	}
    	
    	return inv;
    }
    
    public static void escribirHTML(Invitacion inv, String filename)
    {
    	FileWriter file = null;
    	PrintWriter pw = null;
    	try {
			file = new FileWriter(filename, false);
			pw = new PrintWriter(file);
			
			pw.println("<HTML>");
			pw.println("<HEAD>");
			pw.println("<TITLE> Invitation (sgmlpl/CSS formatting) </TITLE>");
			pw.println("<LINK href=\"invit.css\" rel=\"style-sheet\" type=\"text/css\">");
			pw.println("</HEAD>");
			pw.println("<BODY>");
			pw.println("<H1>INVITATION</H1>");
			pw.println("<P><TABLE>");
			pw.println("<TBODY>");
			pw.println("<TR><TD class=\"front\" style=\"color: blue;\">To: </TD>");
			pw.println("<TD>"+inv.getTo()+"</TD></TR>");
			pw.println("<TR><TD class=\"front\" style=\"color: red;\">When: </TD>");
			pw.println("<TD>"+inv.getDate()+"</TD></TR>");
			pw.println("<TR><TD class=\"front\" style=\"color: green;\">Venue: </TD>");
			pw.println("<TD>"+inv.getWhere()+"</TD></TR>");
			pw.println("<TR><TD class=\"front\" style=\"color: yellow;\">Occasion: </TD>");
			pw.println("<TD>"+inv.getWhy()+"</TD></TR>");
			pw.println("</TBODY>");
			pw.println("</TABLE></P>");
			pw.println(inv.getBody());
			pw.println("<P CLASS=\"signature\">"+inv.getFrom()+"</P>");
			pw.println("</BODY>");
			pw.println("</HTML>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			  try {
		           if (null != file)
		              file.close();
		           } catch (Exception e2) {
		              e2.printStackTrace();
		           }
		}
    }
    
	public static void main( String[] args )
    {
    	if(args.length < 2)
    	{
    		System.out.println("Error, falta el nombre del fichero XML y/o el fichero HTML de salida");
    		
    	}else 
    	{
    		Invitacion inv = parserXML(args[0]);
    		System.out.println(inv.toString());
    		escribirHTML(inv, args[1]);
    	}
    }
}
